#include "message_id_creator.h"
#include "base/logging.h"
#include "message_db_wrapper.h"

namespace imserver {

int64_t MessageIdCreator::createMsgId() {
  {
    boost::mutex::scoped_lock lock(mutex_);
    int64_t curId = 0L;
    if (!MessageDBWrapper::instance().select_tb_message_id_seq(curId)) {
      return -1L;
    }

    if (!MessageDBWrapper::instance().update_tb_message_id_seq()) {
      return -1L;
    }
    ++curId;

    return curId;
  }
}

int64_t MessageIdCreator::createDialogueId() {
  {
    boost::mutex::scoped_lock lock(mutex_);
    int64_t curId = 0L;
    if (!MessageDBWrapper::instance().select_tb_dialogue_id_seq(curId)) {
      return -1L;
    }

    if (!MessageDBWrapper::instance().update_tb_dialogue_id_seq()) {
      return -1L;
    }
    ++curId;

    return curId;
  }
}

}

